package com.controller;

import com.entity.Cart;
import com.entity.Login;
import com.entity.Order;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.BuyerService;
import com.service.ThreeLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/buyer")
public class BuyerController {

    @Autowired
    private BuyerService buyerService;
    @Autowired
    private ThreeLinkService threeLinkService;

    //主页信息页面
    @GetMapping("/index")
    public ModelAndView index(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/buyer/index");
        modelAndView.addObject("productCategoryList", buyerService.findAllProductCatetory());
        modelAndView.addObject("productList", buyerService.findAllProduct());
        Login loginInfo = (Login) session.getAttribute("loginInfo");

        return modelAndView;
    }

    //    产品详情页面
    @GetMapping("/productDetail/{id}")
    public ModelAndView productDetail(@PathVariable Integer id,HttpSession session) {
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/buyer/productDetail");
        modelAndView.addObject("productDetail", buyerService.findProductDetail(id));
        return modelAndView;
    }

    //    产品详情页加入购物车处理
    @GetMapping("/addCart/{id}/{cost}")
    public ModelAndView addCart(@PathVariable Integer id, @PathVariable Integer cost,HttpSession session) {
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        Cart cart = new Cart(id, 1, cost, loginInfo.getId());
        buyerService.addCartInfoByPId(cart);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/buyer/car/1");
        return modelAndView;

    }

    //购物车页面
    @GetMapping("/car/{pageNum}")
    public ModelAndView car1(@PathVariable Integer pageNum,HttpSession session) {
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/buyer/car");

        PageHelper.startPage(pageNum, 3);
        List<Cart> cartList = buyerService.findAllCartByLId(loginInfo.getId());
        PageInfo<Cart> cartPageInfo = new PageInfo<>(cartList);
        modelAndView.addObject("sum",buyerService.findSumCartByLId(loginInfo.getId()));
        modelAndView.addObject("cartList", cartList);
        modelAndView.addObject("cartPageInfo", cartPageInfo);

        return modelAndView;
    }

//    删除购物车信息
    @GetMapping("/deleteCart/{id}")
    public ModelAndView deleteCart(@PathVariable Integer id,HttpSession session){
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        ModelAndView modelAndView = new ModelAndView();
        buyerService.deleteCartByLId(id,loginInfo.getId());
        modelAndView.setViewName("redirect:/buyer/car/1");
        return modelAndView;
    }


    //    商品列表页面
    @GetMapping("/list")
    public ModelAndView list(HttpSession session) {
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("productList", buyerService.findAllProduct());
        modelAndView.setViewName("/buyer/list");
        return modelAndView;
    }

//    支付页面
    @GetMapping("/topay")
    public ModelAndView topay(HttpSession session){
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("addressList",buyerService.findAllAddress(loginInfo.getId()));
        modelAndView.addObject("cartList",buyerService.findAllCartByLId(loginInfo.getId()));
        modelAndView.addObject("threeLinks",threeLinkService.findAll());
        modelAndView.addObject("sum",buyerService.findSumCartByLId(loginInfo.getId()));
        modelAndView.addObject("allSum",buyerService.findSumCartByLId(loginInfo.getId())+10);
        modelAndView.setViewName("/buyer/topay");
        return modelAndView;
    }

//    支付处理
    @GetMapping("/payment/{address}/{sum}")
    public ModelAndView payment(@PathVariable String address,@PathVariable Integer sum,HttpSession session){
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/buyer/paySuccess");
        Order order = buyerService.addOrderInfoByLId(address, sum, loginInfo.getId());
        modelAndView.addObject("newOrderInfo",order);

        return modelAndView;
    }

    @PostMapping("/orderAddress")
    public String orderAddress(Integer pro,Integer city,Integer area,String address_detail,HttpSession session){
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        String allAddress = threeLinkService.findAllNameById(pro, city, area, address_detail);
        buyerService.addAddressInfoByLId(allAddress, loginInfo.getId());
        return "redirect:/buyer/topay";

    }

    @GetMapping("/deleteAddress/{id}")
    public String deleteAddress(@PathVariable Integer id,HttpSession session){
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        buyerService.deleteAddressInfo(id,loginInfo.getId());
        return "redirect:/buyer/topay";
    }

    @GetMapping("/paySuccess")
    public String paySuccess(HttpSession session){
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        return "/buyer/paySuccess";
    }


    //    订单详情页面
    @GetMapping("/orderDetail")
    public String orderDetail(HttpSession session) {
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        return "/buyer/orderDetail";
    }


    //    个人信息页面
    @GetMapping("/selfInfo")
    public String selfInfo(HttpSession session) {
        Login loginInfo = (Login) session.getAttribute("loginInfo");
        return "/buyer/selfInfo";
    }


}
